home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3323 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: news.nstn.ca!news
  2. From: keichele@ac.dal.ca (Klaus Eichele)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: looping & sscanf
  5. Date: Sun, 28 Jan 1996 00:57:39 GMT
  6. Organization: Dalhousie University
  7. Message-ID: <4ee3lb$q3i@news.nstn.ca>
  8. References: <DLtqMw.8Ao@emr1.emr.ca>
  9. NNTP-Posting-Host: rewasylishen.chem.dal.ca
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. jagrant@emr1.emr.ca (John Grant) wrote:
  13.  
  14. >In FORTRAN, I can read a set of numbers from a character string with an
  15. >'internal read':
  16. >    character*100 buffer
  17. >    real x(10)
  18. >    read(buffer,*) (x(i),i=1,n)
  19.  
  20. >How can I do this easily in C?  If I was reading directly from a file, I
  21. >would use:
  22. >    for(i=0;i<n;i++){
  23. >      fscanf(f,"%g",&x[i]);
  24. >    }
  25.  
  26. >However, I want to read the values from a string, i.e. using atof() or
  27. >sscanf() or ???.
  28.  
  29. >I was hoping to do something like:
  30. >    char buffer[100];
  31. >    char *b=buffer;
  32. >    for(i=0;i<n;i++){
  33. >      sscanf(b,"%g",&x[i]);
  34. >      b+=???;
  35. >    }
  36. >but I can't figure out how much to advance the pointer.
  37.  
  38. >Perhaps I should loop through the string using strtok(), converting each
  39. >substring using atof()? It seems a bit messy, well at least not as
  40. >straightforward and simple as FORTRAN.
  41.  
  42. >I'm sure it's straightforward, but I just can't see it (or find it in the FAQ).
  43. >-- 
  44. >John A. Grant                        jagrant@emr1.emr.ca
  45. >Airborne Geophysics
  46. >Geological Survey of Canada, Ottawa
  47.  
  48. Hi John,
  49. before starting, I should warn you that I don't have any of the C
  50. "bibles" around nor have any experience with the following function (I
  51. assume that someone on this list shall set me straight if there is a
  52. problem). However, from the books I DO have around it seems like
  53. strtod() (in stdlib.h) could be the function you are looking for:
  54.  
  55. double strtod(const char *s, char **endp)
  56.  
  57. converts the beginning of string s to type double. If endp is not
  58. NULL, the address of the first character that follows the converted
  59. substring is assigned to *endp.  In case os an error, errno is given a
  60. nonzero value.
  61.  
  62. Good Luck,
  63. Klaus
  64.  
  65.  
  66. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  67. |                Klaus Eichele                    |
  68. |  Department of Chemistry, Dalhousie University  |
  69. |  Halifax, N. S.,                Canada B3H 4J3  |
  70. |          e-mail: keichele@ac.dal.ca             |
  71. |    http://ac.dal.ca/~keichele/keichele.html     |
  72. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  73.  
  74.